package com.jky.stmall.front.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import java.util.Date;

@Component
@Aspect //声明一个切面，由通知和切点
public class MethodTimeAspect {
    @Pointcut("execution(* com.jky.stmall.front.*.*.*(..))")
    public void pointcut(){
        //声明一个方法来确定切点
    }
    //前置通知
    @Before("pointcut()")
    public void before(){
        System.out.println("前置通知");
    }
    @After("pointcut()")
    public void after(){
        System.out.println("后置通知");
    }
    //后置返回通知
    @AfterReturning("pointcut()")
    public void afterReturning(){
        System.out.println("后置返回通知");
    }
    //后置异常通知
    @AfterThrowing("pointcut()")
    public void afterThrow(){
        System.out.println("后置异常通知");
    }

    //环绕通知
    @Around("pointcut()")
    //参数可以得到加环绕的方法一些通知
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        //方法执行前
        Date start=new Date();
        //执行方法
        Object obj=joinPoint.proceed();
        //方法执行后
        Date end=new Date();
        System.out.println(end.getTime()-start.getTime());
        Object target=joinPoint.getTarget();
        System.out.println(target.getClass().getPackage()+"."+target.getClass().getName());
        return obj;
    }
}
